<?php

/**
 * class description
 *
 * 
 * @author Wedesin
 */ 

if ( ! defined( 'ABSPATH' ) ) {
  exit;
}

if( ! class_exists( 'SlickSlider' ) )
{
	class SlickSlider
	{
        
        public function __construct()
		{
			
			//register scripts
            add_action( 'wp_enqueue_scripts', [$this, 'enqueue_scripts'] );
            
            //setup
            add_action( 'after_setup_theme', [$this, 'theme_update'] );

        }

        /**
        * 	Add settings
        *
        * 
        * 	@author Wedesin
        * 	@return echo
        */

        function theme_update() {

            add_image_size( 'slick-thumbnail', 250, 120, true );
            add_image_size( 'slick-large-thumbnail', 650, 450, true );	

        }

        /**
        * 	Add scripts
        * 
        * 	@author Wedesin
        * 	@return 
        */

        function enqueue_scripts() {

            $class_location = "/inc/slickslider/";

            $jc = filemtime( get_stylesheet_directory() . $class_location . 'css/slick.css' );
	        wp_enqueue_style('slick-css', get_stylesheet_directory_uri() . $class_location . 'css/slick.css', array(),	$jc , 'all' );

            $jc = filemtime( get_stylesheet_directory() . $class_location . 'js/slick.js' );
	        wp_enqueue_script('slick-js', get_stylesheet_directory_uri() . $class_location . 'js/slick.js', array('jquery'),	$jc, true );

        }	

        /**
        * 	Create slick slider with one image and navigation with incorporated lightgallery
        * 
        * 	@author Wedesin
        * 	@return echo
        */
        public function gallery_with_main_image( $post_id ) {
			
			// check if the repeater field has rows of data
            if( have_rows('gallery_slider', $post_id) ):
                
				echo '<div class="slick-slider slider-for '. ( class_exists( 'LightGallery' ) ? 'lightgallery' : '' ) .'">';

				// loop through the rows of data
				while ( have_rows('gallery_slider', $post_id) ) : the_row();

					// display a sub field value
					$img_url = get_sub_field('gallery_image');

					if ( !empty( $img_url ) ) {

						echo '<figure '. ( class_exists( 'LightGallery' ) ? 'data-src="'. $img_url['url'] .'"' : '' ) .'>';

							echo '<img src="'.$img_url['sizes']['slick-large-thumbnail'] .'" alt="'.$img_url['alt'].'">
						</figure>';
					}				

				endwhile;

				echo '</div>';

				echo '<div class="slick-slider slider-nav">';

				// loop through the rows of data
				while ( have_rows('gallery_slider') ) : the_row();

					// display a sub field value
					$img_url = get_sub_field('gallery_image');

					if ( !empty( $img_url ) )

						echo '<a href="#">
							<img src="'.$img_url['sizes']['slick-thumbnail'] .'" alt="'.$img_url['alt'].'">
						</a>';

				endwhile;

				echo '</div>';

			endif;

        }

	}

}


new SlickSlider;